package com.mazaiting.mybatisplus.encrypt.encryptor;

import com.mazaiting.mybatisplus.encrypt.IEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * base64 编码
 * @author danghailiang
 */
public class Base64Encryptor implements IEncryptor {
    /**
     * 日志服务
     */
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String encrypt(Object param, String key) throws Exception {
        // 使用base64加密
        return Base64.getEncoder().encodeToString(param.toString().getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String decrypt(Object param, String key) throws Exception {
        // 使用base64解密
        byte[] bytes = Base64.getDecoder().decode(param.toString());
        String decrypt = new String(bytes, StandardCharsets.UTF_8);
        logger.info("Base64Encryptor解密后的decryptParam:{}",decrypt);
        return decrypt;
    }
}
